# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load(
    "//pw_build:pigweed.bzl",
    "pw_cc_test",
)
load("//pw_build:selects.bzl", "TARGET_COMPATIBLE_WITH_HOST_SELECT")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_library(
    name = "poll",
    hdrs = [
        "public/pw_async2/internal/poll_internal.h",
        "public/pw_async2/poll.h",
    ],
    includes = ["public"],
    deps = [
        "//pw_polyfill",
        "//pw_string:to_string",
        "//third_party/fuchsia:stdcompat",
    ],
)

pw_cc_test(
    name = "poll_test",
    srcs = ["poll_test.cc"],
    deps = [
        ":poll",
        "//pw_result",
    ],
)

# NOTE: this target should only be used directly by implementors of the
# `dispatcher` facade.
cc_library(
    name = "dispatcher_base",
    srcs = [
        "dispatcher_base.cc",
    ],
    hdrs = [
        "public/pw_async2/dispatcher_base.h",
    ],
    includes = [
        "public",
    ],
    deps = [
        ":poll",
        "//pw_assert",
        "//pw_chrono:system_clock",
        "//pw_containers:vector",
        "//pw_sync:interrupt_spin_lock",
        "//pw_sync:lock_annotations",
        "//pw_toolchain:no_destructor",
    ],
)

cc_library(
    name = "dispatcher",
    hdrs = [
        "public/pw_async2/dispatcher.h",
    ],
    includes = [
        "public",
    ],
    deps = [
        ":dispatcher_backend",
        ":dispatcher_base",
    ],
)

label_flag(
    name = "dispatcher_backend",
    build_setting_default = ":dispatcher_backend_multiplexer",
)

cc_library(
    name = "dispatcher_backend_multiplexer",
    visibility = ["//targets:__pkg__"],
    deps = select({
        "@platforms//os:linux": ["//pw_async2_epoll:dispatcher"],
        "//conditions:default": ["//pw_async2_basic:dispatcher"],
    }),
)

pw_cc_test(
    name = "dispatcher_test",
    srcs = ["dispatcher_test.cc"],
    deps = [":dispatcher"],
)

pw_cc_test(
    name = "dispatcher_thread_test",
    srcs = ["dispatcher_thread_test.cc"],
    # TODO: b/343776738 - update to run on all compatible devices
    target_compatible_with = select(TARGET_COMPATIBLE_WITH_HOST_SELECT),
    deps = [
        ":dispatcher",
        "//pw_function",
        "//pw_thread:sleep",
        "//pw_thread:thread",
    ],
)

cc_library(
    name = "pend_func_task",
    hdrs = [
        "public/pw_async2/pend_func_task.h",
    ],
    includes = [
        "public",
    ],
    deps = [
        ":dispatcher",
    ],
)

pw_cc_test(
    name = "pend_func_task_test",
    srcs = ["pend_func_task_test.cc"],
    deps = [":pend_func_task"],
)

cc_library(
    name = "pendable_as_task",
    hdrs = [
        "public/pw_async2/pendable_as_task.h",
    ],
    includes = [
        "public",
    ],
    deps = [
        ":dispatcher",
    ],
)

pw_cc_test(
    name = "pendable_as_task_test",
    srcs = ["pendable_as_task_test.cc"],
    deps = [":pendable_as_task"],
)

cc_library(
    name = "allocate_task",
    hdrs = [
        "public/pw_async2/allocate_task.h",
    ],
    includes = [
        "public",
    ],
    deps = [
        ":dispatcher",
        "//pw_allocator:allocator",
    ],
)

pw_cc_test(
    name = "allocate_task_test",
    srcs = ["allocate_task_test.cc"],
    deps = [
        ":allocate_task",
        "//pw_allocator:testing",
    ],
)

cc_library(
    name = "coro",
    hdrs = [
        "public/pw_async2/coro.h",
    ],
    includes = [
        "public",
    ],
    tags = ["requires_cxx_20"],
    deps = [
        ":dispatcher",
        "//pw_allocator:allocator",
    ],
)

pw_cc_test(
    name = "coro_test",
    srcs = ["coro_test.cc"],
    tags = ["requires_cxx_20"],
    deps = [
        ":coro",
        ":dispatcher",
        "//pw_allocator:null_allocator",
        "//pw_allocator:testing",
    ],
)
